/**
 * Doctor's Orders
 */
package docoffice.doss;

import java.sql.ResultSet;
import java.util.logging.Logger;

import docoffice.database.DatabaseManager;


/**
 * Doctor Orders
 * 
 * Will receive the prescription, labwork, and doctor's instructions.
 * @author Jackie Trujillo
 * @version $Revision: 1.0 $
 */
public class DocOrders{
	
	/**
	 * Field instructions.
	 */
	private String instructions;
	
	/**
	 * Field labWork.
	 */
	private LabWork labWork;
	
	/**
	 * Field prescription.
	 */
	private Prescription prescription;
	
	/**
	 * Field LOG.
	 */
	private static final Logger LOG = Logger.getLogger("DocOrders");
	
	/**
	 * Field id.
	 */
	private int id;
	
	/**
	 * Constructor for DocOrders.
	 * @param prescrip String
	 * @param lab String
	 * @param instruct String
	 */
	public DocOrders(Prescription prescrip, LabWork lab, String instruct){
		this.prescription = prescrip;
		this.labWork = lab;
		this.instructions = instruct;
	}

	/** 
	 * Method getPrescription
	
	 * @return String **/
	public Prescription getPrescription() {
		return prescription;
	}

	/**
	 * Method setPrescription.
	 * @param prescription String
	 */
	public void setPrescription(Prescription prescription) {
		this.prescription = prescription;
	}

	/**
	 * Method getLabWork.
	
	 * @return String */
	public LabWork getLabWork() {
		return labWork;
	}

	/**
	 * Method setLabWork.
	 * @param labWork String
	 */
	public void setLabWork(LabWork labWork) {
		this.labWork = labWork;
	}

	/**
	 * Method getInstructions.
	
	 * @return String */
	public String getInstructions() {
		return instructions;
	}

	/**
	 * Method setInstructions.
	 * @param instructions String
	 */
	public void setInstructions(String instructions) {
		this.instructions = instructions;
	}
	
	/**
	 * id getter
	
	 * @return id */
	public int getId(){
		return this.id;
	}
	
	/**
	 * id setter
	 * @param id
	 */
	public void setId(int id){
		this.id = id;
	}
	
	/**
	 * Method createDocOrders.
	 * @param newPrescrip String
	 * @param newLab String
	 * @param newInstruct String
	 */
	public void createDocOrders(Prescription newPrescrip, LabWork newLab, 
			String newInstruct){
		final int prescriptionID = newPrescrip.readPrescriptionID(newPrescrip);
		final String workType = newLab.getWorkType();
		final double cost = newLab.getCost();
		final int labID = newLab.readLabWorkID(workType, cost);
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "INSERT INTO docorders (LabworkID, PrescriptionID, " +
				"Instructions) " + "VALUES (" + labID + ", " + prescriptionID + ", '" + 
				newInstruct + "')";
		
		try{
			dbMgr.runUpdateQuery(sqlQuery);
		} catch(Exception e){
			LOG.severe("A SQL Error occurred while creating docorders");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection();
		}
		
		LOG.info("Docorders created successfully");
	}
	
	/**
	 * Method readDocOrders.
	 * @param docorders
	
	 * @return docorders */
	public DocOrders readDocOrders(DocOrders docorders){
		final int id = docorders.getId();
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "SELECT * FROM docorders WHERE OrderID = " + id;
		ResultSet rs = null;
		
		try{
			rs = dbMgr.runReadQuery(sqlQuery);
			rs.first();
			final int labID = rs.getInt("LabworkID");
			final int prescriptionID = rs.getInt("PrescriptionID");
			final String instructions = rs.getString("Instructions");
			LabWork labwork = new LabWork("", 0);
			labwork.setId(labID);
			labwork = labwork.readLabWork(labwork);
			Prescription prescription = new Prescription("", "");
			prescription.setId(prescriptionID);
			System.out.println(prescriptionID);
			prescription = prescription.readPrescription(prescription);
			docorders.setInstructions(instructions);
			docorders.setLabWork(labwork);
			docorders.setPrescription(prescription);
		} catch(Exception e){
			LOG.severe("A SQL Error occurred while reading docorders");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection(rs);
		}
		
		LOG.info("Docorders were read successfully");
		return docorders;
	}
	
	/**
	 * Method updateDocOrders.
	 * @param docorders DocOrders
	 */
	public void updateDocOrders(DocOrders docorders){
		final int id = docorders.readDocOrdersId(docorders);
		final LabWork labwork = docorders.getLabWork();
		final String instructions = labwork.getWorkType();
		final double cost = labwork.getCost();
		final int labID = labwork.readLabWorkID(instructions, cost);
		final Prescription prescription = docorders.getPrescription();
		final int prescriptionID = prescription.readPrescriptionID(prescription);
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "UPDATE docorders SET LabworkID = " + labID + 
		        ", PrescriptionID = " + prescriptionID + " WHERE OrderID = " + id;
		
		try{
			dbMgr.runUpdateQuery(sqlQuery);
		} catch(Exception e){
			LOG.severe("A SQL Error occurred while updating docorders");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection();
		}
		
		LOG.info("Docorders updated successfully");
	}
	
	/**
	 * Method deleteDocOrders.
	 * @param docorders DocOrders
	 */
	public void deleteDocOrders(DocOrders docorders){
		final int id = docorders.getId();
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "DELETE FROM docorders WHERE OrderID = " + id;
		
		try{
			dbMgr.runUpdateQuery(sqlQuery);
		} catch(Exception e){
			LOG.severe("A SQL Error occurred while deleting docorders");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection();
		}
		
		LOG.info("Docorders deleted successfully");
	}
	
	
	public int readDocOrdersId(DocOrders docorders){
		int id = 0;
		final LabWork labwork = docorders.getLabWork();
		final String instructions = labwork.getWorkType();
		final double cost = labwork.getCost();
		final int labID = labwork.readLabWorkID(instructions, cost);
		final Prescription prescription = docorders.getPrescription();
		final int prescriptionID = prescription.readPrescriptionID(prescription);
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "SELECT OrderID FROM docorders WHERE " +
				"LabworkID = " + labID + " AND PrescriptionID = " + prescriptionID;
		ResultSet rs = null;
		
		try{
			rs = dbMgr.runReadQuery(sqlQuery);
			rs.first();
			id = rs.getInt("OrderID");
		} catch(Exception e){
			LOG.severe("A SQL Error occurred while reading docorder id");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection(rs);
		}
		
		LOG.info("Docorder id read successfully");
		return id;
	}
	
	/**
	 * Method toString.
	
	 * @return String  */
	public String toString(){
		return "Doctor's prescription, instruction, and lab results for the patient";
	}
	
}